package com.campus.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.campus.entity.Login;
import com.campus.entity.User;
import com.campus.service.LoginService;
import com.campus.util.AppMD5Util;
import com.campus.util.ServiceUtil;
import com.campus.util.SessionUtil;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @ClassName LoginAction
 * @Description ��̨��¼��ҳ
 * @author guodayang
 * @date 2018��3��9�� 22:08:10
 */
public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private HttpServletRequest request = ServletActionContext.getRequest();
	protected final String JUMP = "jump";// ��ת
	private Login login;// �˺�
	private String resultJson;// ������� 1��Ч/-1ʧ��/2��ǰҳ��
	private User user;
	
	@Resource
	private LoginService ls;

	/** ��¼ */
	public String login() {
 
		// �ж�����ʽ
		if ("post".equalsIgnoreCase(ServletActionContext.getRequest().getMethod())) {

			String md5Password= AppMD5Util.MD5(login.getPassword());
			user = ls.chekelogin(login.getUsername(),md5Password);
			if (user == null) {
				resultJson = "-1";
				System.err.println("login()"+resultJson);
				return SUCCESS;
			} else {
				resultJson = "1";
				System.err.println("login()"+resultJson);
				return SUCCESS;
			}
		} else {
			System.err.println("login()"+JUMP);
			return JUMP;
		}
	}

	/** �ǳ� */
	public String endLogin() {
		request.getSession().invalidate();// ���session
		return SUCCESS;
	}

	/** ��������ҳ�� */
	public String myUser() {
		// �ж�����ʽ
		if ("post".equalsIgnoreCase(ServletActionContext.getRequest().getMethod())) {
			user = SessionUtil.getMyUserSession();
			return SUCCESS;
		}else {
			return JUMP;
		}
	}

	public Login getLogin() {
		return login;
	}

	public void setLogin(Login login) {
		this.login = login;
	}

	public String getResultJson() {
		return resultJson;
	}

	public void setResultJson(String resultJson) {
		this.resultJson = resultJson;
	}


	public String getJUMP() {
		return JUMP;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

}
